package com.xyzwps.server.paimon.demo

import io.konform.validation.Validation
import io.konform.validation.constraints.maxLength
import io.konform.validation.constraints.minLength
import io.konform.validation.constraints.minimum

data class KfItem(val name: String)

data class KfDemo(val name: String, val age: Int?, val items: List<KfItem>)

val kfiValidator = Validation<KfItem> {
    KfItem::name {
        minLength(2) hint "名字至少2字符"
        maxLength(10)
    }
}

val kfdValidator = Validation<KfDemo> {
    KfDemo::name {
        minLength(2) hint "名字至少2字符"
        maxLength(10)
    }

    KfDemo::age ifPresent {
        minimum(18) hint "年龄不能小于18"
    }

    KfDemo::items onEach {
        runDynamic {
            kfiValidator
        }
    }
}

fun main() {

    run {
        val kfDemo = KfDemo("", 0, listOf(KfItem("")))
        val result = kfdValidator.validate(kfDemo)
        println(result)
    }

    run {
        val kfDemo = KfDemo("11111", 0, listOf(KfItem("")))
        val result = kfdValidator.validate(kfDemo)
        println(result)
    }

    run {
        val kfDemo = KfDemo("11111", 18, listOf(KfItem("")))
        val result = kfdValidator.validate(kfDemo)
        println(result)
    }
}